package qmlt.dataset.filter;

import java.util.ArrayList;
import java.util.List;

import qmlt.dataset.DataSetBase;
import qmlt.dataset.DataSet;
import qmlt.dataset.Instance;
import qmlt.dataset.utils.DataSetUtils;
import qmlt.dataset.utils.ListUtils;

/**
 * Center the feature matrix. Targets (outputs) are simply copied.
 * 
 * @author quyin
 * 
 */
public class CenterFilter implements Filter
{

	@Override
	public DataSet filter(DataSet ds)
	{
		List<Float> means = new ArrayList<Float>();
		for (int i = 0; i < ds.getFeatureDefs().size(); ++i)
		{
			List<Float> values = ListUtils.convert(DataSetUtils.getFeatureColumn(ds.getInstances(), i));
			means.add((float) ListUtils.mean(values));
		}

		DataSet newDs = new DataSetBase(ds.getDef().clone(ds.getId() + "-center"), ds);
		for (Instance inst : ds.getInstances())
		{
			Instance newInst = new Instance(inst.id, newDs);
			for (int i = 0; i < newInst.dataSet.getFeatureDefs().size(); ++i)
			{
				newInst.getFeatures().add((Float) inst.getFeatures().get(i) - means.get(i));
			}
			newInst.setTarget(inst.getTarget());
			newDs.getInstances().add(newInst);
		}

		return newDs;
	}

}
